package cc.flyflow.core.servicetask;

import cc.flyflow.common.dto.process.ProcessInstanceCopyDto;
import cc.flyflow.common.dto.flow.Node;
import cc.flyflow.common.dto.flow.node.CopyNode;
import cc.flyflow.common.utils.JsonUtil;
import cc.flyflow.core.node.AssignUserStrategyFactory;
import cc.flyflow.core.node.NodeDataStoreFactory;
import cc.flyflow.core.utils.BizHttpUtil;
import cc.flyflow.core.utils.FlowableUtils;
import cn.hutool.core.collection.CollUtil;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 抄送任务处理器--java服务任务
 * <p>
 * 拷贝业务任务
 *
 * @author Jyl
 */
public class CopyServiceTask implements JavaDelegate
{

    /**
     * 执行
     *
     * @param execution
     */
    @Override
    public void execute(DelegateExecution execution)
    {
        ExecutionEntityImpl entity = (ExecutionEntityImpl) execution;

        String nodeId = entity.getActivityId();
        String flowId = entity.getProcessDefinitionKey();
        String tenantId = execution.getTenantId();
        String rootUserId = FlowableUtils.getStartUserId(execution);//发起人

        Node node = NodeDataStoreFactory.getInstance().getNode(flowId, nodeId, tenantId);
        if (node != null)
        {
            Map<String, Object> variables = execution.getVariables();
            CopyNode copyNode = (CopyNode) node;
            Integer assignedType = copyNode.getAssignedType();

            List<String> userIdList = AssignUserStrategyFactory.getStrategy(assignedType).handle(node, rootUserId,
                    variables, tenantId);
            if (CollUtil.isNotEmpty(userIdList))
            {
                //发送抄送任务
                ProcessInstanceCopyDto copyDto = new ProcessInstanceCopyDto();
                copyDto.setNodeTime(new Date());
                copyDto.setStartUserId(rootUserId);
                copyDto.setFlowId(flowId);
                copyDto.setProcessInstanceId(execution.getProcessInstanceId());

                copyDto.setNodeId(nodeId);
                copyDto.setNodeName(node.getNodeName());
                copyDto.setFormData(JsonUtil.toJSONString(variables));
                copyDto.setUserIdList((userIdList));
                copyDto.setTenantId(tenantId);

                BizHttpUtil.saveCC(copyDto);
            }
        }
    }
}
